#ifndef t_bsearch_h
#define t_bsearch_h
/*
 * Sylvain BERTRAND <digital.ragnarok@gmail.com>
 * code protected by GNU GPL v3
 */
G_BEGIN_DECLS
#define BSEARCH_SETUP(t) register t l_BSEARCH, r_BSEARCH, out_BSEARCH;
#define BSEARCH(t, ar, start, size, val) \
{ \
    l_BSEARCH = (start);              \
    r_BSEARCH = (start)+(size)-1;     \
    while (l_BSEARCH <= r_BSEARCH) { \
        out_BSEARCH = l_BSEARCH + (r_BSEARCH - l_BSEARCH)/2;      \
        if ((val) == (ar)[out_BSEARCH]) {                           \
            break; \
        } \
        else if ((val) < (ar)[out_BSEARCH] && out_BSEARCH > 0) {     \
            r_BSEARCH = out_BSEARCH-1; \
        } \
        else \
            l_BSEARCH = out_BSEARCH+1; \
    } \
}
#define BSEARCH_FOUND() (l_BSEARCH <= r_BSEARCH)
#define BSEARCH_AT() (out_BSEARCH)
G_END_DECLS
#endif
